package ide.ccf.ccproject.data;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;

import ide.ccf.ccproject.common.Utils;
import ide.ccf.ccproject.translate.Message;
import ide.ccf.ccproject.wizards.pages.ExportSelectFolderWizardPage;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.w3c.dom.Document;

public class SetDesktop {
	protected Shell shell;  
	protected Object result;
	private Document document;
	final Display display = Display.getDefault();
	public Text tName, tGenericName, tCotent, tIcon, tVersion, tExec;
	public Combo cTerminal, cHide, cMenuCategory, cTypeOfMenu;
	public int flag = 0;
	public String sName;
	public SetDesktop(Shell parent) {
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            this.document = builder.newDocument();
        } catch (ParserConfigurationException e) {
            System.out.println(e.getMessage());
        }
	}
	
	 public Object open() { 
	        createContents();  
	        shell.open();  
	        shell.layout();   
	        while (!shell.isDisposed()) {  
	            if (!display.readAndDispatch())  
	                display.sleep();  
	        }  
	        return result;  
	    }
	 
		public void close(){
			 shell.close();
		 }
		
		public void createDesktop(String filePath) throws IOException
		{
			Utils.setProfileString(filePath, "name", tName.getText());
			Utils.setProfileString(filePath, "GenericName", tGenericName.getText());
			Utils.setProfileString(filePath, "Comment",  tCotent.getText());
			Utils.setProfileString(filePath, "Type",  cMenuCategory.getText());
			Utils.setProfileString(filePath, "Icon",  tIcon.getText());
			Utils.setProfileString(filePath, "Version",  tVersion.getText());
			Utils.setProfileString(filePath, "Exec",  tExec.getText());
			Utils.setProfileString(filePath, "Terminal",  cTerminal.getText());
			Utils.setProfileString(filePath, "Hidden",  cHide.getText());
			Utils.setProfileString(filePath, "Category",  cTypeOfMenu.getText());
		}

	private void createContents() {
		  shell = new Shell(display, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);  
	      shell.setSize(550, 650);  
	      shell.setText(Message.ExportSetInfoWizardPage_2);  
	      shell.setLayout(new FillLayout());
	      
	        final ScrolledComposite sc =  new ScrolledComposite(shell, SWT.H_SCROLL  
	                | SWT.V_SCROLL | SWT.BORDER);  
	        final Composite c = new Composite(sc, SWT.NONE);  
	        sc.setContent(c);  
	        GridLayout layout = new GridLayout();  
	        layout.numColumns = 1;  
	        c.setLayout(layout);  
	        GridData gdText = new GridData(SWT.FILL, SWT.FILL, false, false, 1, 1);
		    gdText.heightHint = 580;
		    c.setLayoutData(gdText);
	    	final Group g_desktop = new Group(c, SWT.NONE);
	    	g_desktop.setText("菜单启动器信息");
			GridData gd_desktop = new GridData(GridData.FILL_BOTH);
			gd_desktop.horizontalSpan = 1;
			g_desktop.setLayoutData(gd_desktop);
			GridLayout layout_desktop = new GridLayout(2,false);
			layout_desktop.verticalSpacing = 10;
			g_desktop.setLayout(layout_desktop);
			{
				Label name = new Label(g_desktop, SWT.NONE);
				name.setText("启动器显示名：");
				tName = new Text(g_desktop, SWT.BORDER);
				Label genericName = new Label(g_desktop, SWT.NONE);
				genericName.setText("启动器通用名称：");
				tGenericName = new Text(g_desktop, SWT.BORDER);
				Label cotent = new Label(g_desktop, SWT.NONE);
				cotent.setText("启动器内容简介：");
				tCotent = new Text(g_desktop, SWT.BORDER);
				Label category = new Label(g_desktop, SWT.NONE);
				category.setText("启动器菜单类型：");
				cMenuCategory = new Combo(g_desktop, SWT.BORDER);
				cMenuCategory.setItems(new String[]{"Application","Link"});
				cMenuCategory.setText("Application");
				Label icon = new Label(g_desktop, SWT.NONE);
				icon.setText("启动器图标文件：");
				tIcon = new Text(g_desktop, SWT.BORDER);
				Label version = new Label(g_desktop, SWT.NONE);
				version.setText("应用程序版本号：");
				tVersion = new Text(g_desktop, SWT.BORDER);
				Label exec = new Label(g_desktop, SWT.NONE);
				exec.setText("主程序启动命令：");
				tExec = new Text(g_desktop, SWT.BORDER);
				Label terminal = new Label(g_desktop, SWT.NONE);
				terminal.setText("是否在终端运行：");
				cTerminal = new Combo(g_desktop, SWT.BORDER);
				cTerminal.setItems(new String[]{"是","否"});
				cTerminal.setText("是");
				Label hide = new Label(g_desktop, SWT.NONE);
				hide.setText("菜单是否隐藏：");
				cHide = new Combo(g_desktop, SWT.BORDER);
			    cHide.setItems(new String[]{"是","否"});
			    cHide.setText("是");
				Label type = new Label(g_desktop, SWT.NONE);
				type.setText("菜单所属类别：");
				cTypeOfMenu = new Combo(g_desktop, SWT.BORDER);
				cTypeOfMenu.setItems(new String[]{"AudioVideo","Audio", "Video", "Development", "Education", 
						"Game", "Graphics", "Network", "Office", "Science", "Settings", "System", "Utility"});
				cTypeOfMenu.setText("AudioVideo");

				   Composite s = new Composite(c, SWT.NONE);
			        GridData gd_s = new GridData(GridData.FILL_BOTH);
					s.setLayoutData(gd_s);
					GridLayout layout_s= new GridLayout(4,false);
					layout_s.verticalSpacing = 7;
					layout_s.horizontalSpacing = 80;
					s.setLayout(layout_s);
					Label occupy1 = new Label(s, SWT.NONE);
					occupy1.setText("");
					Label occupy2 = new Label(s, SWT.NONE);
					occupy2.setText("");
					Button s_1  = new Button(s, SWT.PUSH);
					s_1.setText("取  消");
					s_1.addSelectionListener(new SelectionAdapter()
					{
						@Override
						public void widgetSelected(SelectionEvent e) {
							flag = 0;
							close();
						}
					});
					Button s_2 = new Button(s, SWT.PUSH);
					s_2.setText("生  成");
					s_2.addSelectionListener(new SelectionAdapter()
					{
						@Override
						public void widgetSelected(SelectionEvent e) {
							String pathSFolder = ExportSelectFolderWizardPage.sfoldernamePath_cache;
							 sName = tName.getText().trim();
							 Utils.generalCPK(pathSFolder);
						        if(!sName.isEmpty())
						        {
							        String str=pathSFolder  + "/CPK/"+  sName +".desktop";
							     	try {
										initDesktop(str);
									} catch (IOException e2) {
										e2.printStackTrace();
									}
						            try {
										createDesktop(str);
									} catch (IOException e1) {
										e1.printStackTrace();
									}
							        flag = 1;
							        close();
						        }
						        else
						        { 
						        	MessageBox messageBox =   
						        			   new MessageBox(shell,   
						        			    SWT.OK|   
						        			    SWT.CANCEL|   
						        			    SWT.ICON_WARNING);  
						        			 messageBox.setMessage("启动器名不能为空！");   
						        			 messageBox.open();  
						        }
						}

						private void initDesktop(String strPath) throws IOException {
							File f = new File(strPath);
							String str="[Desktop Entry] \r\nName=\r\nGenericName=\r\nComment=\r\nType=\r\nIcon=\r\nVersion=\r\nExec=\r\nTerminal=\r\nHidden=\r\nCategory=\r\n" ;
						  if(!f .exists()){
							  f .createNewFile();
						  }
						  byte bytes[]=new byte[512];
						   bytes=str.getBytes();  
						   int b=str.length();  
						   FileOutputStream fos=new FileOutputStream(f );
						  fos.write(bytes,0,b);
						  fos.close();
						}
					});
				
				c.setSize(c.computeSize(500, 580));
				tName.setSize(tName.computeSize(300, 22));
				tGenericName.setSize(tGenericName.computeSize(300, 22));
				tCotent.setSize(tCotent.computeSize(300, 22));
				cMenuCategory.setSize(cMenuCategory.computeSize(310, 22));
				tIcon.setSize(tIcon.computeSize(300, 22));
				tVersion.setSize(tVersion.computeSize(300, 22));
				tExec.setSize(tExec.computeSize(300, 22));
				cTerminal.setSize(cTerminal.computeSize(310, 22));
				cHide.setSize(cHide.computeSize(310, 22));
				cTypeOfMenu.setSize(cTypeOfMenu.computeSize(310, 22));
				s_1.setSize(s_1.computeSize(100, 30));
				s_2.setSize(s_2.computeSize(100, 30));
				occupy1.setSize(occupy1.computeSize(120, 30));
				occupy2.setSize(occupy2.computeSize(120, 30));
			}
	}

	public String getFileName() {
		return  sName+".desktop";
	}  
}
